package com.netty.im.service;

import com.netty.encodedecode.memreport.DirectMemReporter;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Created by Administrator on 2018/9/9/009.
 */
public class ImServer {
    public static void main(String[] args) {
        EventLoopGroup bossLoop = new NioEventLoopGroup();
        EventLoopGroup workLoop = new NioEventLoopGroup();

        try {
            //服务端启动类
            ServerBootstrap bootStrap = new ServerBootstrap();
            bootStrap.group(bossLoop, workLoop)
                    .channel(NioServerSocketChannel.class)
                    //针对于bossLoop来说的
//                    .handler(null)
                    //针对于workLoop来说的。bossLoop接受到请求之后，会将连接转发给workLoop，通过此handler来进行真正的处理。
                    .childHandler(new ImServiceInitializer());

            ChannelFuture channelFuture = bootStrap.bind(8811).sync();
            new DirectMemReporter().startReport();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossLoop.shutdownGracefully();
            workLoop.shutdownGracefully();
        }
    }
}
